﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace DreamShieldBinding
{
    /// <summary>
    /// Base Helper Class for the project objects
    /// </summary>
    public class DSXBaseObject
    {
        /// <summary>
        /// Element xml node
        /// </summary>
        protected virtual XmlNode Node { get; set; }

        protected DSXBaseObject(XmlNode node)
        {
            if (node == null)
                throw new ArgumentNullException("node");

            Node = node;
        }

        #region Utility for structured elements (tree)

        /// <summary>
        /// Delete the current xml element
        /// </summary>
        protected virtual void DeleteCurrent()
        {
            Node.ParentNode.RemoveChild(Node);
            Node = null; /* don't try to access the object when deleted */
        }

        #endregion

        #region Attributes Helper

        /*
         *  Just to get easily Xml Attributes
         * */

        /// <summary>
        /// Try to get an attribute
        /// </summary>
        /// <param name="attributeName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        protected virtual bool TryGetAttribute(string attributeName, out string value)
        {
            value = null;
            foreach (XmlAttribute attrib in Node.Attributes)
            {
                if (attrib != null && String.Equals(attrib.Name, attributeName))
                {
                    value = attrib.Value;
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Get an attribute value or return a default value
        /// </summary>
        /// <param name="attributeName"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        protected virtual string GetAttribute(string attributeName, string defaultValue)
        {
            string result;
            if (TryGetAttribute(attributeName, out result))
                return result;
            else
                return defaultValue;
        }

        /// <summary>
        /// Set an attribute (create it if necessary)
        /// </summary>
        /// <param name="attributeName"></param>
        /// <param name="value"></param>
        protected virtual void SetAttribute(string attributeName, string value)
        {
            /* First : check if the attribute exists */
            string dumb;
            if (TryGetAttribute(attributeName, out dumb))
            {
                /* ok */
                Node.Attributes[attributeName].Value = value;
            }
            else
            {
                /* we need to create it */
                var attribute = Node.OwnerDocument.CreateAttribute(attributeName);
                attribute.Value = value;

                Node.Attributes.Append(attribute);
            }
        }

        #endregion

        #region Elements helpers


        #endregion
    }
}
